package com.yimi.appstore.adapter;

import java.io.File;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.yimi.appstore.R;
import com.yimi.appstore.download2.DownloadEntity;
import com.yimi.appstore.download2.DownloadJob;
import com.yimi.appstore.download2.DownloadManager;
import com.yimi.appstore.download2.FileService;
import com.yimi.appstore.util.FileUtils;
import com.yimi.appstore.util.Log;
import com.yimi.appstore.util.Util;

public class DownloadJobAdapter extends ExpandableAdapter<DownloadJob> {

	public DownloadJobAdapter(Context context, List<DownloadJob> listData) {
		super(context, listData);
	}

	class ViewHolder {
		public ImageView appIcon;
		public TextView appName;
		public TextView appSizeStatus;
		public TextView downloadSpeed;
		public TextView downloadPercent;
		public ProgressBar progressBar;
	}

	@Override
	public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
		final DownloadJob job = listData.get(groupPosition);
		View row = convertView;
		final ViewHolder holder;
		if (row == null || row.getTag() == null) {
			holder = new ViewHolder();
			LayoutInflater inflater = ((Activity) context).getLayoutInflater();
			row = inflater.inflate(R.layout.download_item_layout, null);
			holder.appIcon = (ImageView) row.findViewById(R.id.download_app_icon);
			holder.appName = (TextView) row.findViewById(R.id.download_app_name);
			holder.appSizeStatus = (TextView) row.findViewById(R.id.download_app_size_status);
			holder.downloadPercent = (TextView) row.findViewById(R.id.download_app_percent);
			holder.progressBar = (ProgressBar) row.findViewById(R.id.download_app_pb);
			holder.downloadSpeed = (TextView) row.findViewById(R.id.download_speed);
			row.setTag(holder);

		} else {
			holder = (ViewHolder) row.getTag();
		}
		job.setmPb(holder.progressBar);
		job.setMappSizeStatus(holder.appSizeStatus);
		job.setMdownloadPercent(holder.downloadPercent);
		job.setMdownloadSpeed(holder.downloadSpeed);
		if (job.getDownloadEntry() != null) {
			final DownloadEntity de = job.getDownloadEntry();
			holder.appName.setText(de.getResource());
			Log.d("", "de.getCurrentBytes()>>" + de.getCurrentBytes());
			if (de.getCurrentBytes() == de.getTotalBytes() && de.getTotalBytes() != 0) {
				holder.downloadPercent.setText("100%");
				holder.progressBar.setVisibility(View.GONE);
				holder.appSizeStatus.setText("下载完成");
			} /*else if (job.getJobStatus() == DownloadManager.STATE_ERROR) {
				holder.progressBar.setVisibility(View.GONE);
				holder.appSizeStatus.setText("获取资源失败");
				holder.downloadPercent.setVisibility(View.GONE);
			} */else {
				holder.progressBar.setVisibility(View.VISIBLE);
				holder.progressBar.setMax(100);
				if (de.getTotalBytes() == (long) 0) {
					de.setTotalBytes(1);
					holder.appSizeStatus.setText("未知");
				} else {
					holder.appSizeStatus.setText(FileUtils.convertStorage(de.getCurrentBytes()) + "/"
							+ FileUtils.convertStorage(de.getTotalBytes()));
				}
				holder.progressBar.setProgress((int) ((de.getCurrentBytes() * 100) / de.getTotalBytes()));
				if (job.getJobStatus() == DownloadManager.STATE_PAUSE) {
					holder.downloadPercent.setText("已暂停       " + (de.getCurrentBytes() * 100) / de.getTotalBytes()
							+ "%");
				} else if (job.getJobStatus() == DownloadManager.STATE_READY_DOWNLOAD) {
					holder.downloadPercent.setText("正在连接       " + (de.getCurrentBytes() * 100) / de.getTotalBytes()
							+ "%");
				} else if (job.getJobStatus() == DownloadManager.STATE_PAUSEING) {
					holder.downloadPercent.setText("正在暂停       " + (de.getCurrentBytes() * 100) / de.getTotalBytes()
							+ "%");
				} else if (job.getJobStatus() == DownloadManager.STATE_WAITING) {
					holder.downloadPercent.setText("等待中       " + (de.getCurrentBytes() * 100) / de.getTotalBytes()
							+ "%");
				}else if (job.getJobStatus() == DownloadManager.STATE_ERROR) {
					holder.downloadPercent.setText("网络出错       " + (de.getCurrentBytes() * 100) / de.getTotalBytes()
							+ "%");
				}

			}

		}
		return row;
	}

	@Override
	public View getChildView(final int groupPosition, int childPosition, boolean isLastChild, View convertView,
			ViewGroup parent) {
		final DownloadJob job = listData.get(groupPosition);
		View view = lInflater.inflate(R.layout.app_download_bottom_layout, null);
		TextView leftTv = (TextView) view.findViewById(R.id.download_left_btn);
		if (job.getJobStatus() == DownloadManager.STATE_FINISH)
			leftTv.setText("安装");
		else if (job.getJobStatus() == DownloadManager.STATE_INSTALLED)
			leftTv.setText("打开");
		else
			leftTv.setText("详情");
		leftTv.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (job.getJobStatus() == DownloadManager.STATE_FINISH) {
					Util.installApk(context, job.getDownloadEntry().getLocalPath());
					job.setJobStatus(DownloadManager.STATE_INSTALLED);
				} else if (job.getJobStatus() == DownloadManager.STATE_INSTALLED) {

				} else {
				}
			}
		});
		TextView middleTv = (TextView) view.findViewById(R.id.download_mid_btn);
		if (job.getJobStatus() == DownloadManager.STATE_DOWNLOADING) {
			middleTv.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.btn_icon_paused, 0, 0);
			middleTv.setText("暂停");
		} else if (job.getJobStatus() == DownloadManager.STATE_PAUSE || job.getJobStatus() == DownloadManager.STATE_ERROR) {
			middleTv.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.btn_icon_download, 0, 0);
			middleTv.setText("继续下载");
		} else if (job.getJobStatus() == DownloadManager.STATE_FINISH) {
			middleTv.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.btn_icon_download, 0, 0);
			middleTv.setText("重新下载");
		}
		middleTv.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (job.getJobStatus() == DownloadManager.STATE_DOWNLOADING
						|| job.getJobStatus() == DownloadManager.STATE_READY_DOWNLOAD) {
					DownloadManager dm = DownloadManager.getInstance(context);
					dm.pauseDownloadTask(groupPosition);
				} else if (job.getJobStatus() == DownloadManager.STATE_PAUSE || job.getJobStatus() == DownloadManager.STATE_ERROR) {
					DownloadManager dm = DownloadManager.getInstance(context);
					job.setJobStatus(DownloadManager.STATE_WAITING);
					dm.startDownloadTask();
				} else if (job.getJobStatus() == DownloadManager.STATE_FINISH
						|| job.getJobStatus() == DownloadManager.STATE_INSTALLED) {
					DownloadManager dm = DownloadManager.getInstance(context);
					dm.restartDownloadTask(job, groupPosition);
				}
			}
		});

		view.findViewById(R.id.download_right_btn).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				AlertDialog.Builder b = new Builder(context);
				View contentView = ((Activity) context).getLayoutInflater().inflate(R.layout.delete_dialog_layout, null);
				final CheckBox checkBox = (CheckBox)contentView.findViewById(R.id.is_delete);
				b.setPositiveButton("确定", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						if(checkBox.isChecked()){
							File file = new File(job.getDownloadEntry().getLocalPath());
							if(file.exists())
								file.delete();
						}
						FileService fileService = new FileService(context);
						fileService.delete(job.getDownloadEntry().getUrl());
						DownloadManager dm = DownloadManager.getInstance(context);
						dm.cancleDownloadTask(groupPosition);
					}
				});
				b.setNegativeButton("取消", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				});
				b.setIcon(R.drawable.ssl_icon);
				b.setTitle("温馨提示");
				b.setView(contentView);
				b.create().show();
			}
		});
		return view;
	}
	

}
